package com.zxzhu.joyread.View;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.zxzhu.joyread.Common.BaseActivity;
import com.zxzhu.joyread.Presenter.ILoginPresenter;
import com.zxzhu.joyread.Presenter.LoginPresenter;
import com.zxzhu.joyread.R;
import com.zxzhu.joyread.Util.PermissionsUtil;

public class LoginView extends BaseActivity implements ILoginView {

    private Button loginButton;
    private TextView registerButton;
    private EditText userEdit,pswEdit;
    private ProgressDialog dialog;
    private ILoginPresenter presenter;
    private ImageButton qq,wb;
    private static final String APP_ID = "1106236205";
    private static final String TAG = "LoginView";

    @Override
    protected void initData() {
        loginButton = $(R.id.login_loginButton);
        registerButton = $(R.id.login_registerButton);
        userEdit = $(R.id.login_username);
        pswEdit = $(R.id.login_password);
        qq = $(R.id.qqLogin);
        wb = $(R.id.wbLogin);
        presenter = new LoginPresenter(this,this);
        setLoginButton();
        setRegisterButton();
        setClick();
        //setWebLogin();
    }
    private void setRegisterButton() {
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.gotoRegisterView();
            }
        });
    }

    private void setLoginButton() {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = userEdit.getText().toString();
                String password = pswEdit.getText().toString();
                if(!username.isEmpty() && !password.isEmpty()){
                    presenter.login(username,password);
                }
                else toast("账号或密码不能为空!");
            }
        });
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login_view;
    }

    @Override
    public void initState() {
//        super.initState();
    }

    @Override
    public void showDialog() {
        if(dialog == null) dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
        dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
        dialog.setCanceledOnTouchOutside(true);// 设置在点击Dialog外是否取消Dialog进度条
        dialog.setTitle("正在网络加载");
        dialog.setMessage("请稍等");
        dialog.show();
    }

    @Override
    public void hideDialog() {
        if(dialog == null) return;
        dialog.hide();
    }

    @Override
    public void setClick() {
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PermissionsUtil.hasDiskPermission(LoginView.this,0)) {
                    presenter.qqLogin();
                }
            }
        });
        wb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PermissionsUtil.hasDiskPermission(LoginView.this,1)) {
                    presenter.wbLogin();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
